import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kizitonwose.calendar.compose.WeekCalendar
import com.kizitonwose.calendar.compose.weekcalendar.rememberWeekCalendarState
import com.kizitonwose.calendar.core.WeekDay
import com.kizitonwose.calendar.core.atStartOfMonth
import com.kizitonwose.calendar.core.firstDayOfWeekFromLocale
import java.time.LocalDate
import java.time.YearMonth
import java.time.format.TextStyle
import java.util.Locale

/*
*author:guyueyuan
*createTime:2024/11/18
*/

@Composable
fun WeekScroll(
    currentCheckedDay:LocalDate=LocalDate.now(),
    currentMonth: YearMonth =YearMonth.now(),
    firstVisibleWeekDay: LocalDate =LocalDate.now(),
    changeCurrentCheckedDay:(LocalDate)->Unit
) {

    val firstDayOfWeek = remember { firstDayOfWeekFromLocale() } // Available from the library
    val state = rememberWeekCalendarState(
//    开始时间
    startDate = currentMonth.atStartOfMonth(),
//    结束时间
    endDate = currentMonth.atEndOfMonth(),
//    默认展示周
    firstVisibleWeekDate = firstVisibleWeekDay,
//    一周的第一天
    firstDayOfWeek = firstDayOfWeek
    )

    WeekCalendar(
        state = state,
        modifier = Modifier
            .background(color = Color(0xff6BE8F2)),
        dayContent = { Day(it,currentCheckedDay,changeCurrentCheckedDay) }
    )
}

@Composable
fun Day(it: WeekDay,currentCheckedDay:LocalDate,changeCurrentCheckedDay:(LocalDate)->Unit) {
    Column(
        modifier = Modifier
            .padding(8.dp)
            .width(IntrinsicSize.Max)
            .clickable {
                changeCurrentCheckedDay(it.date)
            },
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.SpaceEvenly
    ) {
        // 显示星期几
        Text(
            text = it.date.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH).substring(0,3),
            style = MaterialTheme.typography.bodyLarge,
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            color = Color.Gray
        )
        // 显示日号
        Text(
            text = it.date.dayOfMonth.toString(),
            style = MaterialTheme.typography.bodyMedium,
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            color =if(it.date==currentCheckedDay) Color.Blue else if (it.date==LocalDate.now()) Color.Red else Color.Black
        )
    }
}

@Preview
@Composable
fun WeekScrollPre() {
    WeekScroll(changeCurrentCheckedDay = {})
}